/*
pthread_rwlock_init(&rwlock,nullptr)
pthread_rwlock_rdlock(&rwlock)
pthread_rwlock_wrlock(&rwlock)
pthread_rwlock_unlock(&rwlock)
pthread_rwlock_destroy(&rwlock)
*/

#include <iostream>
#include <unistd.h>
#include <pthread.h>

using namespace std;


pthread_rwlock_t rwlock;
int counter = 0;

void* readCounter(void* arg)
{
	sleep(1);
	pthread_rwlock_rdlock(&rwlock);
	cout << "counter:" << counter << endl;
	pthread_rwlock_unlock(&rwlock);
	
	return nullptr;
}

void* writeCounter(void* arg)
{
	pthread_rwlock_wrlock(&rwlock);
	counter++;
	pthread_rwlock_unlock(&rwlock);

	return nullptr;
}


int main(int argc,char** argv)
{
	pthread_t t1,t2,t3;
	
	pthread_rwlock_init(&rwlock,nullptr);

	pthread_create(&t1,nullptr,readCounter,nullptr);
	pthread_create(&t2,nullptr,writeCounter,nullptr);
	pthread_create(&t3,nullptr,readCounter,nullptr);

	pthread_join(t1,nullptr);
	pthread_join(t2,nullptr);
	pthread_join(t3,nullptr);

	pthread_rwlock_destroy(&rwlock);
	return 0;
}





